package com.tau.iplayer.client.dialogs;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.tau.iplayer.client.IPlayer;
import com.tau.iplayer.client.IPlayerUtils;
import com.tau.iplayer.client.models.Playlist;
import com.tau.iplayer.client.models.Track;

public class AddTrackDialog extends DialogBox {

	private static final boolean IS_AUTO_HIDE = false;
	private static final boolean IS_MODAL = false;
	private static final boolean IS_ANIMATED = true;
	private static final boolean IS_GLASS = false;
	private static final String HEADER = "Add Track";

	private Track track;
	private IPlayer iPlayer;
	private ListBox playlistListBox;
	private TextBox artistTextBox;
	private TextBox titleTextBox;

	public AddTrackDialog(IPlayer iplayer) {
		this();
		this.iPlayer = iplayer;
	}

	public AddTrackDialog() {
		setAutoHideEnabled(IS_AUTO_HIDE);
		setModal(IS_MODAL);
		setAnimationEnabled(IS_ANIMATED);
		setGlassEnabled(IS_GLASS);
		setText(HEADER);
	}

	/**
	 * @param track
	 *            the track to set
	 */
	public void setTrack(Track track) {
		this.track = track;
		createContents();
	}

	private void createContents() {
		DockPanel dockPanel = new DockPanel();

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		final Button addSongButton = new Button("Add Song");
		addSongButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				handleAddSongClicked();
			}
		});
		addSongButton.setStyleName("button");
		Button previewButton = new Button("Preview");
		previewButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				handlePreviewClicked();
			}
		});
		previewButton.setStyleName("button");
		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("button");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleCancelClick();
			}
		});
		horizontalPanel.add(addSongButton);
		horizontalPanel.add(previewButton);
		horizontalPanel.add(cancelButton);
		horizontalPanel.setSpacing(3);
		dockPanel.add(horizontalPanel, DockPanel.SOUTH);
		dockPanel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_CENTER);

		Grid formGrid = new Grid(4, 2);
		formGrid.setText(0, 0, "Title:");
		formGrid.getCellFormatter().setStyleName(0, 0, "standardLabel");
		titleTextBox = new TextBox();
		titleTextBox.setText(track.getTitle());
		titleTextBox.setStyleName("addSongDialogTextBox");
		titleTextBox.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				boolean isEnabled = !IPlayerUtils.isEmpty(titleTextBox
						.getValue());
				addSongButton.setEnabled(isEnabled);
			}
		});
		formGrid.setWidget(0, 1, titleTextBox);

		formGrid.setText(1, 0, "Artist:");
		formGrid.getCellFormatter().setStyleName(1, 0, "standardLabel");
		artistTextBox = new TextBox();
		if(track.getArtist() != null && track.getArtist().length() > 0){
			artistTextBox.setText(track.getArtist());
		}
		artistTextBox.setStyleName("addSongDialogTextBox");
		formGrid.setWidget(1, 1, artistTextBox);

		formGrid.setText(2, 0, "Playlist:");
		formGrid.getCellFormatter().setStyleName(2, 0, "standardLabel");
		playlistListBox = new ListBox();
		for (Playlist playlist : iPlayer.getPlaylists()) {
			playlistListBox.addItem(playlist.getName());
		}
		playlistListBox.setStyleName("standardLabel");
		Playlist currentPlaylist = iPlayer.getCurrentPlaylist();
		if (currentPlaylist != null) {
			int i = iPlayer.getPlaylistListBoxIndex(playlistListBox,
					currentPlaylist);
			if (i != -1) {
				playlistListBox.setItemSelected(i, true);
			}
		}
		formGrid.setWidget(2, 1, playlistListBox);

		formGrid.setText(3, 0, "Duration:");
		formGrid.getCellFormatter().setStyleName(3, 0, "standardLabel");
		formGrid.setText(3, 1, IPlayerUtils.formatTime(track.getDuration()));
		formGrid.getCellFormatter().setStyleName(3, 1, "standardLabel");
		dockPanel.add(formGrid, DockPanel.WEST);

		Image thumbnailImage = new Image(track.getThumbnailURL());
		dockPanel.add(thumbnailImage, DockPanel.CENTER);

		setWidget(dockPanel);
		thumbnailImage.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				handleThumbnailOnLoad();
			}
		});
	}

	private void handleAddSongClicked() {
		track.setTitle(titleTextBox.getValue());
		track.setArtist(artistTextBox.getValue());
		String playlistName = playlistListBox.getValue(playlistListBox
				.getSelectedIndex());
		iPlayer.addTrack(track, new Playlist(playlistName));
		this.hide();
	}

	private void handlePreviewClicked() {
		iPlayer.loadTrack(track, true);
	}

	protected void handleThumbnailOnLoad() {
		setVisible(true);
	}

	private void handleCancelClick() {
		this.hide();
	}
}
